<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <base href="<%=basePath%>">
<title>中介公司配置</title>

<jsp:include page="/WEB-INF/jsp/common/common.jsp"></jsp:include>
<link rel="stylesheet" href="<%=path%>/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.ztree.excheck-3.5.js"></script>
<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
	history.forward();
	$(document).ready(function() {
		isReadyIframe();
		queryAgencyInfo(1);
	});
	
	function isReadyIframe(){
		var iframe = $(window.parent.document).find("#iframe").length;
		if(iframe==0){
			window.location.href="<%=basePath%>index.jsp";
		}
	}
	//点击“查询”
	function queryAgencyInfo(pageIndex){
		var agencyCode = $.trim($("#agencyCode").val());
		var agencyName = $.trim($("#agencyName").val());
		$.openLoading();
		//按照条件执行查询操作
		$.ajax({
			url : "<%=path%>/agencyManage/queryByCriteria?"+new Date().getTime(),
			type : 'POST',
			async : true,
			dataType:'json',
			data:{
				agencyCode:agencyCode,
				agencyName:agencyName,
				pageIndex:pageIndex
			},
			success:function(agencyInfoPage){
				$.closeLoading();
				
				if($.validate(agencyInfoPage))  return;
				
				if(null != agencyInfoPage){
					var buttonStr = "<a href='' onClick='viewAgencyInfo(this);return false'>编辑</a> <a href='' onClick='deployPower(this);return false'>配置权限</a> <a href='' onClick='deleteAgencyInfo(this);return false'>删除</a>";
					var fieldNames = "AGENCY_CODE,AGENCY_NAME,DEPT_CODE@TDH,DEPT_CNAME";
					
					var tableContent = creatTable1(fieldNames,agencyInfoPage,null,buttonStr);
					var tableObj = $("#agencyInfoTable");
					tableObj.find("tbody").remove();
					tableObj.append(tableContent);
					
					var strPage = creatChannelPage(pageIndex,6,agencyInfoPage.maxPage,"24");
					var pageDIV = $("#agencyInfoPageDIV");
					pageDIV[0].innerHTML="";
					pageDIV.append(strPage);
				}
			}
		});
	}
	
	//关闭  窗口
	function closeCurrentDIV(obj){
			$("#"+obj).CloseDiv();
	}
	
	<%-- 配置权限  begin --%>
	var agencyCodeForRelated="";
	var deptCode="";
	//打开配置机构 div
	function deployPower(obj){
		var delTr = $(obj).parent('td').parent('tr')[0];
		agencyCodeForRelated = delTr.childNodes[0].innerHTML;
		deptCode = delTr.childNodes[2].innerHTML;
		channelInfoTree();
	}
	
 	//展开树形 机构
	function openChannelInfoTree(event, treeId, treeNode){
		if(treeNode.isParent){
			var isOpen = treeNode.open;
			channelInfoTreeObj.expandNode(treeNode,!isOpen);
		}
	}
	
	//获取选中的机构
	var deptIdStr;
	function chooseDepts(e, treeId, treeNode) {
	var sNodes = channelInfoTreeObj.getCheckedNodes();
	if(sNodes.length>0){
		for(var i=0; i<sNodes.length;i++){
			<%--配置 权限特殊处理--%>
			if(deptCode != sNodes[i].id){
					deptIdStr+= sNodes[i].id+',';
				}
			}
		}
	}
	
	//保存 角色机构配置
	function saveDept(){
		deptIdStr='';
		chooseDepts();
		if(deptIdStr==''){
			$.message("error","请至少配置一个合作伙伴");
			return;
		}
		$.openLoading();
		$.ajax({
			url: "<%=path%>/agencyManage/bindChannelInfoListsToAgencyChannel",
			type: "post",
			dataType: "json",
			data:{"deptIdStr":deptIdStr,
				  "agencyCode":agencyCodeForRelated,
				  "deptCodeStr":deptCode
				  },
			async:true,
			success: function(data) {
				$.closeLoading();
				if($.validate(data))return;
				closeCurrentDIV('pDeptDiv');
			}
		});
	}
	
	//初始化树形菜单
	function channelInfoTree(){
		var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				view: {
					dblClickExpand: true
				},
				callback: {
					onClick: openChannelInfoTree
				},
				check: {
					enable: true,
					chkStyle: "checkbox",
					chkboxType: { "Y": "s", "N": "s" }
				}
			};
		$.openLoading();
		$.ajax({
			url: "<%=path%>/agencyManage/getChannelInfoListTree",
			type: "post",
			dataType: "json",
			data:{"deptCode":deptCode},
			async:true,
			cache:false,
			success: function(page) {
				$.closeLoading();
				if($.validate(page))return;
				//业务处理
					channelInfoTreeObj = $.fn.zTree.init($("#deptTreeDemo"), setting, page.data);
					$.ajax({
						url: "<%=path%>/agencyManage/getChannelInfosOfCurrAgency",
						type: "post",
						dataType: "json",
						data:{"agencyCode":agencyCodeForRelated},
						async:true,
						success: function(data) {
							if($.validate(data))return;
							//业务处理
								if(data!=null){
									$.each(data,function(i,item){
										try{channelInfoTreeObj.checkNode(channelInfoTreeObj.getNodeByParam("id", item.channelCode, null), true, true);
										}catch(e){}
									})
								}else{
									$.message("error", "合作伙加载失败");
								}
						$('#pDeptDiv').OpenDiv();
						}
					});
			}
		});
	}
	<%-- 配置权限  end --%>
	
	<%--保存(add | update)saveAgencyInfo--%>
	function saveAgencyInfo(){
		var agencyCode_div = $.trim($("#agencyCode_div").val());
		var agencyName_div = $.trim($("#agencyName_div").val());
		var deptCname_div = $.trim($("#deptCname_div").val());
		var deptCode_div = $.trim($("#deptCode_div").val());
		
		if(agencyCode_div.length == 0){
			$.message("error", "请输入中介公司代码");
			return;
		}
		if(agencyName_div.length == 0){
			$.message("error", "请输入中介公司名称");
			return;
		}
		if(deptCname_div.length == 0){
			$.message("error", "请选择所属分公司");
			return;
		}
		
		var readonly = $("#agencyCode_div").attr("readonly");
		var flag;
		if(readonly == undefined){
			flag = "save";
		}else if(readonly == 'readonly'){
			flag = "update";
		}else{
			return;
		}
		if("save" == flag){
		//再次检查系统中是否已经存在输入的angencyCode
		checkAgencyCodeIsExist();
		if(!agencyCodeFlag){
			return;
			}
		}
		$.openLoading();
		$.ajax({
			url : "<%=path%>/agencyManage/saveAgencyInfo?"+new Date().getTime(),
			type : 'POST',
			async : true,
			data:{
				agencyCode:agencyCode_div,
				agencyName:agencyName_div,
				deptCode:deptCode_div,
				flag:flag
 			},
			dataType:'json',
			success : function(data) {
				$.closeLoading();
				if($.validate(data))  return;
				queryAgencyInfo(1);
				closeAddOrUpdateDiv();
			}
		});
	}
	
	//点击"编辑"
	function viewAgencyInfo(obj){
		var tempTr = $(obj).parent().parent()[0];
		var agencyCode = tempTr.childNodes[0].innerHTML;
		var agencyName = tempTr.childNodes[1].innerHTML;
		var deptCode = tempTr.childNodes[2].innerHTML;
		var deptName = tempTr.childNodes[3].innerHTML;
		
 		$("#agencyCode_div").val(agencyCode);
		$("#agencyName_div").val(agencyName);
		$("#deptCode_div").val(deptCode);
		$("#deptCname_div").val(deptName);
		
		$("#agencyCode_div").attr("readonly","readonly");
		$("#agencyCode_div").css("background","#ccc");
		$("#agencyCode_div").unbind("blur");
		$("#agencyCode_div_span").css("display","none");
		$("#chooseBranchCode").attr("disabled","disabled");
		$("#chooseBranchCode").css("background","gray");
		$("#addOrUpdateDiv").OpenDiv();
	}
	
	var agencyCodeFlag = true;
	function checkAgencyCodeIsExist(){
		var agencyCode = $("#agencyCode_div").val();
		 $.ajax({
				url : "<%=path%>/agencyManage/queryAgencyInfoByAgencyCode?"+new Date().getTime(),
				type : 'POST',
				async: false,
				dataType:'json',
				data:{
					agencyCode:agencyCode
				},
				success :  function(data) {
					if(data != null && data.result == "fail"){//错误
						$.message("error", "系统正忙，请稍后重试！");
					}else if(data.type != null){//业务处理
						$.message("error", "该中介公司代码已被占用，请更换");
						agencyCodeFlag = false;
						$("#agencyCode_div").focus();
					}else if(data.type == null){
						agencyCodeFlag = true;
					}
				}
			});	 
	}
	
	 
	//点击"新增"(弹出层)
	function addAgencyInfo(){
		$("#agencyCode_div").val("");
		$("#agencyCode_div").removeAttr("readonly");
		$("#agencyCode_div").css("background","#fff");
		$("#agencyCode_div").bind("blur",function (){ checkAgencyCodeIsExist() });
		$("#agencyCode_div_span").css("display","inline");
		$("#chooseBranchCode").removeAttr("disabled");
		$("#chooseBranchCode").css("backgroundImage","url('./images/button_1.png')");
		
		$("#addOrUpdateDiv").OpenDiv();
	}
	
	//点击“删除”（agencyInfo）
	function deleteAgencyInfo(obj){
		var agencyCode = $(obj).parent('td').parent('tr')[0].childNodes[0].innerHTML;
		var deptCode = $(obj).parent('td').parent('tr')[0].childNodes[2].innerHTML;

		if(agencyCode){
			$.dialog({
				type: "warn",//success,error
				content: "您确定要删除该条记录吗？",
				onOk: function() {
					$.openLoading();
					 $.ajax({
							url : "<%=path%>/agencyManage/deleteAgencyInfoByCode?"+new Date().getTime(),
							type : 'POST',
							async: true,
							dataType:'json',
							data:{
								agencyCode:agencyCode,
								deptCode:deptCode
							},
							success : function(data) {
								$.closeLoading();
								if($.validate(data)) return;
								queryAgencyInfo(1);
							}
						});	 
				}
			});
		}
	}
	
	//隐藏 addOrUpdateDiv
	function closeAddOrUpdateDiv(){
		$("#addOrUpdateDiv").CloseDiv();
		$("input[name='agencyCode_div']").val("");
		$("input[name='agencyName_div']").val("");
		$("#deptCname_div").val("");
		$("#deptCode_div").val("");
		$("#agencyCode_div").removeAttr("readonly");
		$("#agencyCode_div").css("background","#fff");
		$("#chooseBranchCode").removeAttr("disabled");
		$("#chooseBranchCode").css("backgroundImage","url('./images/button_1.png')");
	}
	
</script>

  </head>
  <body>
 	 <div class="current">
           <table class="current_table_1">
             <tbody>
              	<tr class="f2">
	                <td><img src="./images/current_1.jpg"></td>
	                <td class="current_table_1_td">
	                  <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
	                  <span style=" color:#136bab; ">合作伙伴管理
	                  	<span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>中介公司配置
	                  </span>
                  	</td>
                 	<td><img src="./images/current_3.jpg"></td>
              	</tr>
           	 </tbody>
           </table>
     </div>
  	
  	<table border="1" id="channelTable" class="table_02" style="height:150px">
  		<tr height="50px">
  			<td colspan="2" align="center">
				 中介公司代码：&nbsp;<input id="agencyCode" type="text" value="" class="input_text" />&nbsp;&nbsp;
				 中介公司名称：&nbsp;<input id="agencyName" type="text" value="" class="input_text" />&nbsp;&nbsp;
				 <input type="button" id="searchButton" class="input_button_1"  value="查询" onclick="queryAgencyInfo(1)"/>
				 <input type="button" id="addButton" class="input_button_1"  value="新增" onclick="addAgencyInfo()"/>&nbsp;&nbsp;
			</td>
  		</tr>
  		<tr>
  			<td valign="top">
	  			<div id="channelInfoDIV" style="width:98%;margin:5px" >
			  		<table class="table_02" id="agencyInfoTable">
			  		<thead>
		              <tr>
		                  <th style="width:25%">中介公司代码</th>
		                  <th style="width:30%">中介公司名称</th>
		                  <th style="width:30%">所属分公司</th>
		                  <th style="width:15%">操作</th>
		              </tr>
		              </thead>
		            </table>
	  				 <div id="agencyInfoPageDIV"  class="page"></div>
	  			</div>
  			</td>
  		</tr>
  	</table>
  	
  	<%--添加或修改agencyInfo相关数据弹出层 --%>
  	<div id="addOrUpdateDiv" style="display: none;width: 60%;height:33%; padding-bottom: 10px;margin-top: -50px">
  		<div class="table_title_div">
				<table class="table_title" style='width:100%;height: 20px;'>
					<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="<%=path %>/images/closedown.gif" alt="关闭"  onclick="closeAddOrUpdateDiv()"/></td>
					</tr>
				</table>
		</div>
		
		<table class="table_02" align="center" style="height: 65%;width: 95%" >
			<tr>
				<td style="text-align:right;border-right:0px;" width="30%">中介公司代码：</td>
				<td style="text-align:left;" width="70%">&nbsp;
					<input id="agencyCode_div" name="agencyCode_div" maxlength="30" class="input_text" style="width: 60%"/><span id="agencyCode_div_span" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td style="text-align:right;border-right:0px;" width="30%">中介公司名称：</td>
				<td style="text-align:left" width="70%">&nbsp;
					<input id="agencyName_div" name="agencyName_div" maxlength="80" class="input_text" style="width: 60%"/><span class="red">*</span>
				</td>
			</tr>
			<tr>
				<td style="border-right: 0px;text-align: right;" nowrap="nowrap">所属分公司：</td>
		   		<td style="padding:5px 13px;text-align: left" nowrap="nowrap">
		   			<input class="input_text" id="deptCname_div" style="text-align: left;width: 271px" readonly="readonly" style="background: #ccc"/>
		   			<input id = "chooseBranchCode" type="button" class="input_button_1" onclick="loadDeptTree()" value="选择"/>
		   			<input type="hidden" id="deptCode_div" />
		   		</td>
			</tr>
		</table>
		
		<div align="center">
		   <input type="button" value="保存" onclick="saveAgencyInfo()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
	       <input type="button" value="取消" onclick="closeAddOrUpdateDiv()" class="input_button_3"/>
		</div>
	</div>
  	<%--deptTree(导入所属分公司层) --%>
    <jsp:include page="/WEB-INF/jsp/common/deptBranch.jsp"></jsp:include>
    <!--树形权限配置 div -->
	<div id="pDeptDiv" style="border:0px solid red;display:none;text-align: center;width:360px; ">
	<div>
			<div class="table_title_div">
			<table class="table_title" style='width:100%;height: 20px;'>
						<tr>
						<td width="10%"></td>
						<td width="80%" id="interfaceName"></td>
						<td width="10%" align="right"><img src="<%=path %>/images/closedown.gif" alt="关闭"  onclick="closeCurrentDIV('pDeptDiv')"/></td>
						</tr>
					</table>
			</div>
			<div id="tt" style="border:0px solid yellow;height:300px;width:100%;overflow-y:auto;">
			<ul id="deptTreeDemo" class="ztree" ></ul>
			</div>
			<table class="table_02" style="height:30px;width:100%;" align="center" >
		</table>
		<div  style='width:100%;'>
		     <input type="button" id="saveBtn" value="确定" onclick="saveDept()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		     <input type="button" value="取消" onclick="closeCurrentDIV('pDeptDiv')" class="input_button_3"/>
		</div>
	</div>
	</div>
  </body>
</html>
